﻿using Common;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.NutritionalRisk
{
    public partial class PrintNutritionalRisk : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        DateTime Start;
        DateTime End;
        ElectronicSignature es;
        #endregion

        #region 构造函数
        public PrintNutritionalRisk(Tb_User loginUser, PatientInfo patientInfo, DateTime StartDate, DateTime EndDate)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            Start = StartDate;
            End = EndDate;
            es = new ElectronicSignature(this.LoginUser);
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\泉州市第一医院住院患者营养风险筛查评估单.grf");
            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //连接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 连接报表取数事件
        private void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(this.PatientInfo.PATIENT_ID, this.PatientInfo.VISIT_ID, (int)FormType.Pabulum, Start, End);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record1 = list[i];                    
                    Nutritions ncs = XmlUtil.XMLToObject<Nutritions>(record1.NursingFormData, Encoding.UTF8);
                    string image1 = "";
                    string HSQM = ncs.HuShiQianMing;
                    if (!string.IsNullOrEmpty(HSQM))
                    {
                        image1 = es.GetElectricSignBase64(record1.AreaId, ncs.HuShiQianMing);
                    }
                    dt.Rows.Add(
                        PatientInfo.PATIENT_NAME,  //患者姓名
                        PatientInfo.SEX,   //性别
                        PatientInfo.AGE,   //年龄
                        PatientInfo.NURSE_CELL_NAME,   //病区
                        PatientInfo.DEPARTMENT_NAME,   //科室
                        PatientInfo.BED_NO,  //床号
                        PatientInfo.PATIENT_ID,   //住院号
                        PatientInfo.DIAGNOSE,  //诊断

                        ncs.JiChuZiLiao_TiZhong,        //体重
                        ncs.JiChuZiLiao_TiZhongZhiShu,    //体重指数
                        ncs.JiChuZiLiao_DanBaiZhi,    //蛋白质
                        ncs.PingGuRiQi,    //评估日期
                        ncs.ZongFen,   //总分
                        ncs.HuShiQianMing,    //护士签名
                        ncs.ShenGao,    //身高
                        
                        ncs.JiBingZhuangTai.Substring(0, 1) == "0" ? " " : "1" ,//疾病状态
                        ncs.JiBingZhuangTai.Substring(2, 1) == "0" ? " " : "2" ,
                        ncs.JiBingZhuangTai.Substring(4, 1) == "0" ? " " : "3" ,

                        ncs.YingYangZhuangTai.Substring(0, 1) == "0" ? " " : "0" ,   //营养状态
                        ncs.YingYangZhuangTai.Substring(2, 1) == "0" ? " " : "1" ,
                        ncs.YingYangZhuangTai.Substring(4, 1) == "0" ? " " : "2" ,
                        ncs.YingYangZhuangTai.Substring(6, 1) == "0" ? " " : "3" ,

                        ncs.NianLing == "0" ? "1" : "",   //年龄
                        
                        ncs.ChuLi.Substring(0, 1) == "0" ? "" : "√",   //处理
                        ncs.ChuLi.Substring(2, 1) == "0" ? "" : "√"
                        );
                }
                //将 DataTable 的数据转储到 Grid++Report 的数据集中
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("PATIENT_NAME", typeof(string));
            dt.Columns.Add("SEX", typeof(string));
            dt.Columns.Add("AGE", typeof(string));
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));
            dt.Columns.Add("BED_NO", typeof(string));
            dt.Columns.Add("PATIENT_ID", typeof(string));
            dt.Columns.Add("DIAGNOSE", typeof(string));

            dt.Columns.Add("JiChuZiLiao_TiZhong", typeof(string));
            dt.Columns.Add("JiChuZiLiao_TiZhongZhiShu", typeof(string));
            dt.Columns.Add("JiChuZiLiao_DanBaiZhi", typeof(string));
            dt.Columns.Add("AssessmentDate", typeof(DateTime));
            dt.Columns.Add("ZongFen", typeof(string));
            dt.Columns.Add("HuShiQianMing", typeof(string));
            dt.Columns.Add("ShenGao", typeof(string));

            dt.Columns.Add("JiBingZhuangTai_1", typeof(string));
            dt.Columns.Add("JiBingZhuangTai_2", typeof(string));
            dt.Columns.Add("JiBingZhuangTai_3", typeof(string));

            dt.Columns.Add("YyingYangZhuangTai_1", typeof(string));
            dt.Columns.Add("YyingYangZhuangTai_2", typeof(string));
            dt.Columns.Add("YyingYangZhuangTai_3", typeof(string));
            dt.Columns.Add("YyingYangZhuangTai_4", typeof(string));

            dt.Columns.Add("NianLing", typeof(string));

            dt.Columns.Add("ChuLi_1", typeof(string));
            dt.Columns.Add("ChuLi_2", typeof(string));
            

        }
        #endregion
        
        /// <summary>
        /// 检查数据项目
        /// </summary>
        /// <param name="str"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        private bool ChkStr(string str, int len)
        {
            if (string.IsNullOrEmpty(str)) return false;
            if (str.IndexOf("#") < 0) return false;
            if (str.Split('#').Length < len) return false;
            return true;
        }
        #region 窗体加载启动锐浪报表
        private void PrintNutritionalRisk_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
    }
}
